package cn.edu.wfit.api.config;

import cn.edu.wfit.common.core.constant.SecurityConstants;
import cn.edu.wfit.common.core.context.SecurityContextHolder;
import cn.edu.wfit.common.core.utils.ServletUtils;
import cn.edu.wfit.common.core.utils.StringUtils;
import cn.edu.wfit.common.core.utils.ip.IpUtils;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;

import java.util.Map;

public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
              /* Long userId = SecurityContextHolder.getUserId();
                String userName = SecurityContextHolder.getUserName();
                String userKey = SecurityContextHolder.getUserKey();

                requestTemplate.header(SecurityConstants.USER_KEY, userKey);
                requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
                requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId.toString());*/
                HttpServletRequest httpServletRequest = ServletUtils.getRequest();
                if (StringUtils.isNotNull(httpServletRequest))
                {
                    Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
                    // 传递用户信息请求头，防止丢失
                    String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
                    if (StringUtils.isNotEmpty(userId))
                    {
                        requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
                    }
                    String userKey = headers.get(SecurityConstants.USER_KEY);
                    if (StringUtils.isNotEmpty(userKey))
                    {
                        requestTemplate.header(SecurityConstants.USER_KEY, userKey);
                    }
                    String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
                    if (StringUtils.isNotEmpty(userName))
                    {
                        requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
                    }
                    String nickName = headers.get(SecurityConstants.DETAILS_NICKNAME);
                    if (StringUtils.isNotEmpty(nickName))
                    {
                        requestTemplate.header(SecurityConstants.DETAILS_NICKNAME, nickName);
                    }
                    String userType = headers.get(SecurityConstants.DETAILS_USER_TYPE);
                    if (StringUtils.isNotEmpty(userType))
                    {
                        requestTemplate.header(SecurityConstants.DETAILS_USER_TYPE, userType);
                    }
                    String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
                    if (StringUtils.isNotEmpty(authentication))
                    {
                        requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
                    }

                    // 配置客户端IP
                    requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr());
                }
            }
        };
    }
}
